import * as turf from '@turf/turf'
import type { LngLat } from './lnglat'

const distOpt = {
  units: 'meters' as turf.Units
}

export function distance(p0: LngLat, p1: LngLat): number {
  return turf.distance([p0.lng, p0.lat], [p1.lng, p1.lat], distOpt)
}

export function bearing(p0: LngLat, p1: LngLat): number {
  return turf.bearing([p0.lng, p0.lat], [p1.lng, p1.lat])
}

export function destination(p0: LngLat, bearing: number, dist: number): LngLat {
  const f = turf.destination([p0.lng, p0.lat], dist / 1000.0, bearing)
  const coord = f.geometry.coordinates
  return {
    lng: coord[0],
    lat: coord[1]
  }
}
